package cn.com.rocketmq.schedual;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.logging.org.slf4j.Logger;
import org.apache.rocketmq.logging.org.slf4j.LoggerFactory;

/**
 * 延迟生产者
 * @author xuhaixiang
 * @date 2023-09-10
 */
public class Producer {

    /**
     * 日志对象
     */
    private static  final Logger logger = LoggerFactory.getLogger(Producer.class);


    public static void main(String[] args) {
        DefaultMQProducer producer = null;
        try {
            producer = new DefaultMQProducer("syncProducer");
            producer.setNamesrvAddr("192.168.113.101:9876");
            producer.start();
            for (int i = 0; i < 10; i++) {
                String body = "Hello zhang " + i;
                //参数一：主题、参数二：过滤、参数三：消息内容
                Message message = new Message("rocketmq_syncDemo","tag", body.getBytes("UTF-8"));
                //1-18 对应 1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h （消费者会跟生产者设置的时间延迟接收消息）
                //message.setDelayTimeLevel(3);

                message.setDelayTimeMs(10000L);
                SendResult result = producer.send(message);
                String msgId = result.getMsgId();
                SendStatus sendStatus = result.getSendStatus();
                logger.info("{}消息发送状态为{}", msgId, sendStatus);
            }
        } catch (Exception e) {
            logger.error("生产者发送消息失败！" ,e);
        }
    }
}
